package com.ljt.service.impl;

import com.ljt.entity.UserEntity;
import com.ljt.repository.UserDao;
import com.ljt.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by wb-ljt251780 on 2017/7/24.
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Override
    public Page<UserEntity> findAll(Pageable pageable) {
        return userDao.findAll(pageable);
    }

    @Override
    public UserEntity findById(Long id) {
        return userDao.findOne(id);
    }

    @Override
    public void saveOrUpdate(UserEntity userEntity) {
        if(userEntity.getId()!=null){
            UserEntity one = userDao.findOne(userEntity.getId());
            one.setEmail(userEntity.getEmail());
            one.setStatus(userEntity.getStatus());
            one.setName(userEntity.getName());
            one.setGender(userEntity.getGender());
            userDao.saveAndFlush(one);
        }else{
            userDao.save(userEntity);
        }
    }

    @Override
    public int login(UserEntity userEntity){
        UserEntity result = userDao.findByUsername(userEntity.getUsername());
        if(result==null){
            throw new RuntimeException("用户名不存在");
        }
        if (result.getPassword().equals(userEntity.getPassword())){

        }
        return 1;
    }
}
